
//
//  XHVideoNextPlayView.m
//  ShowYork
//
//  Created by hxh on 2020/9/7.
//  Copyright © 2020 hxh. All rights reserved.
//

#import "VideoNextPlayView.h"

typedef void(^doneBlock)(void);

@interface VideoNextPlayView ()<TTTAttributedLabelDelegate>
@property (nonatomic,strong)TTTAttributedLabel *titleLb;
@property (nonatomic,strong)UIButton *cancelBtn;
@property (nonatomic,strong)UIButton *confirmBtn;
@property (nonatomic,strong)dispatch_queue_t quene;
@property (nonatomic,strong)dispatch_source_t timer;
@property (nonatomic,copy)doneBlock confirmDone;
@property (nonatomic,copy)doneBlock cancelDone;
@property (nonatomic,copy)doneBlock nextDone;
@end

@implementation VideoNextPlayView

- (instancetype)initWithConfirmAciton:(void (^)(void))confirmBlock cancelAction:(void (^)(void))cancelBlock nextAction:(void (^)(void))nextBlock{
    if (self = [super init]) {
        [self setUI];
        if (confirmBlock) {
            self.confirmDone = ^{
                confirmBlock();
            };
        }
        if (cancelBlock) {
            self.cancelDone = ^{
                cancelBlock();
            };
        }
        if (nextBlock) {
            self.nextDone = ^{
                nextBlock();
            };
        }
    }
    return self;
}

- (void)setUI{
    
    CGFloat maxW = kScreenW;
    CGFloat maxH = kScreenH;
    
    if ([UIApplication sharedApplication].statusBarOrientation == UIDeviceOrientationPortrait ||
    [UIApplication sharedApplication].statusBarOrientation == UIDeviceOrientationPortraitUpsideDown) {
        //当前竖屏
        maxW = kScreenH;
        maxH = kScreenW;
    }
    
    
    
    self.frame = CGRectMake(0, 0, maxW, maxH);
    self.backgroundColor = RGBA(0, 0, 0, 0.8);
    
    self.titleLb = [[TTTAttributedLabel alloc]initWithFrame:CGRectZero];
    self.titleLb.numberOfLines = 0;
    self.titleLb.delegate = self;
    self.titleLb.textColor = [UIColor whiteColor];
    self.titleLb.font = [UIFont systemFontOfSize:14];
    self.titleLb.linkAttributes = @{(NSString *)kCTForegroundColorAttributeName:[RPAppManager themColor],
    (NSString *)kCTUnderlineStyleAttributeName:[NSNumber numberWithBool:NO]};
    [self addSubview:self.titleLb];
    
    self.titleLb.text = @"5秒后继续下一个课程播放，点此直接播放";
    NSRange selRange1 = [self.titleLb.text rangeOfString:@"点此直接播放"];
    [self.titleLb addLinkToTransitInformation:@{@"data":@""} withRange:selRange1];
    self.titleLb.delegate = self;
    
    self.cancelBtn = [[UIButton alloc]init];
    [self.cancelBtn setTitle:@"再次播放" forState:UIControlStateNormal];
    [self.cancelBtn setTitleColor:RGBA(102, 102, 102, 1) forState:UIControlStateNormal];
    self.cancelBtn.titleLabel.font = font(14);
    self.cancelBtn.backgroundColor = [UIColor whiteColor];
    [self.cancelBtn addTarget:self action:@selector(cancelMethod) forControlEvents:UIControlEventTouchUpInside];
    self.cancelBtn.layer.cornerRadius = 17;
    self.cancelBtn.layer.masksToBounds = YES;
    [self addSubview:self.cancelBtn];

    self.confirmBtn = [[UIButton alloc]init];
    [self.confirmBtn setTitle:@"写课后练习" forState:UIControlStateNormal];
    [self.confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.confirmBtn.titleLabel.font = font(14);
    self.confirmBtn.backgroundColor = [RPAppManager themColor];
    [self.confirmBtn addTarget:self action:@selector(confirmMethod) forControlEvents:UIControlEventTouchUpInside];
    self.confirmBtn.layer.cornerRadius = 17;
    self.confirmBtn.layer.masksToBounds = YES;
    [self addSubview:self.confirmBtn];
           
    [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.centerY.equalTo(self).offset(-50);
    }];
    
    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self).offset(-70);
        make.size.mas_equalTo(CGSizeMake(120, 34));
        make.top.equalTo(self.titleLb.mas_bottom).offset(55);
    }];
   
    [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self).offset(70);
        make.size.mas_equalTo(CGSizeMake(120, 34));
        make.centerY.equalTo(self.cancelBtn);
    }];
}

- (void)confirmMethod{
    // 取消倒计时
    dispatch_cancel(self.timer);
    [self dismiss];
    
    if (self.confirmDone) {
        self.confirmDone();
    }
    
}
- (void)cancelMethod{
    dispatch_cancel(self.timer);
    [self dismiss];
    if (self.cancelDone) {
        self.cancelDone();
    }
}

- (void)show{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    
    __block NSInteger second = 5;
    self.quene = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,self.quene);
    dispatch_source_set_timer( self.timer, DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
    dispatch_source_set_event_handler( self.timer, ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            if (second == 0) {
                
                self.titleLb.text = @"5秒后继续下一个课程播放，点此直接播放";
                second = 5;
                dispatch_cancel( self.timer);
                
                [self dismiss];
                
                if (self.nextDone) {
                    self.nextDone();
                }
                
            } else {
                self.titleLb.text = [NSString stringWithFormat:@"%lu秒后继续下一个课程播放，点此直接播放",second];
                second--;
            }
            NSRange selRange1 = [self.titleLb.text rangeOfString:@"点此直接播放"];
            [self.titleLb addLinkToTransitInformation:@{@"data":@""} withRange:selRange1];
            
        });
    });
    dispatch_resume( self.timer);
}

- (void)dismiss{
    [self removeFromSuperview];
}
#pragma mark - 超链接的点击代理方法
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithTransitInformation:(NSDictionary *)components {
    self.titleLb.text = @"5秒后继续下一个课程播放，点此直接播放";
    dispatch_cancel(self.timer);
    [self dismiss];
    if (self.nextDone) {
        self.nextDone();
    }
    
}

@end
